#pragma once

// GameScreen
#include "GameScreen.h"

// For video texture
#include "wmvideotexture.h"
#include "OgreExternalTextureSourceManager.h"

namespace GamePipeGame
{
	class CowTippingScreen : public GamePipe::GameScreen
	{
	public:
		CowTippingScreen(std::string name);
		~CowTippingScreen(); 

		bool LoadResources();
		bool UnloadResources();

		bool Show();

		bool Initialize();
		bool Destroy();

		bool Update();
		bool Draw();

		bool OnKeyPress(const GIS::KeyEvent &keyEvent);
		bool OnKeyRelease(const GIS::KeyEvent &keyEvent);

		enum GoblinTurningState
		{
			NOT_TURNING,
			TURNING_RIGHT,
			TURNING_LEFT,
		};

		enum GoblinState
		{
			IDLE = 0,
			WALK,
			ATTACK
		};

		enum CameraType
		{
			NONE = 0,
			DEBUG,
			FIRST_PERSON,
			THIRD_PERSON,
			REAL_TIME_STRATEGY,

			NUM_CAMERAS
		};

	private:
		struct GrassVertex
		{
			float x, y, z;
			float nx, ny, nz;
			float u, v;
		};

		class CowTippingCamera
		{
		public:
			Ogre::Camera* pCamera;
			int viewportIndex;
			float viewportLeft, viewportTop, viewportWidth, viewportHeight;

			CowTippingCamera() :
				pCamera(0),
				viewportIndex(-1),
				viewportLeft(0.0f),
				viewportTop(0.0f),
				viewportWidth(1.0f),
				viewportHeight(1.0f)
			{
			}
		};

		class Cow
		{
		public:
			Cow(Ogre::SceneManager* pSceneManager, Ogre::Vector3 position, Ogre::Degree rotationDegrees);
			Ogre::Vector3 GetPosition() const { return m_pSceneNode->getPosition(); }
			bool CanBeTipped() { return !m_isTipped && !m_isTipping; }
			void Tip();
			void Update(Ogre::Real timeSinceLastUpdate);

		private:
			static int s_numCowsCreated;
			Ogre::SceneNode* m_pSceneNode;
			bool m_isTipping;
			bool m_isTipped;
			Ogre::Degree m_pitch;
		};

		static const int NUM_COWS = 3;

		Ogre::StaticGeometry* m_pGrassField;
		Ogre::SceneNode* m_pGoblinNode;
		Ogre::AnimationState* m_pGoblinAnimState;
		GoblinState m_GoblinState;
		Ogre::Vector3 m_GoblinHeading;
		bool m_GoblinMoving;
		GoblinTurningState m_GoblinTurningState;
		CowTippingCamera m_cameras[NUM_CAMERAS];
		CameraType m_currentActiveCamType;
		bool m_GoblinThoughtsSoundPlaying;
		Cow* m_cows[NUM_COWS];
		Ogre::CWMVideoTexture* m_pVideoTexture;
		bool m_VideoIsPlaying;

		void SetUpGrass();
		void WaveGrass(Ogre::Real timeElapsed);
		void CreateGrassMesh();
		void CreateCows();
		void DoCowTipping();
		void UpdateGoblin(Ogre::Real timeSinceLastUpdate);
		void GoblinOnKeyPress(const GIS::KeyEvent &keyEvent);
		void GoblinOnKeyRelease(const GIS::KeyEvent &keyEvent);
		void SwitchGoblinAnim(GoblinState state, bool loopAnimation = true);
		void ShowCamera(CameraType type, bool setAsActiveCam = true, const char* compositorName = 0);
		void UpdateMusic();
	};
}